<?php
/**********************************************************************
 * Filename: common.php
 * 
 * Common php functions used throughout the web application, along with 
 * common header variables used throughout thw website.  If javascript or
 * style sheet dependancies change, this is the only file which will need 
 * to be changed.
 * 
 * Author: Phillip Peterson
 * Date Created: 09/13/2009
 *
 **********************************************************************/

//This allows errors to be printed on the page.  This needs to be commented
//out for the commercial version
ini_set ('display_errors', 1);
error_reporting(E_ERROR | E_WARNING | E_PARSE);

//Requirement for sending an email on orion.
require_once "Mail.php";

// Global variables used
$query_success = 1;
$query_fail    = 0;

$g_server     = "http://" . cur_web_dir();
$g_login_page = "/index.php";
$g_nodraft_page = "/no_draft.php";
$g_noschedule_page = "/no_schedule.php";
$g_champ = "/league_champ.php";

$header_open  = "<html xmlns='http://w3.org/1999/xhtml'>";
$header_open .= "<head>";
$header_open .= "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />";
$header_open .= "<link rel='shortcut icon' href='images/favicon.ico' type='image/x-icon' />";
$header_open .= "<title>MTU Fantasy Broomball</title>";
$header_close = "</head>";

//Setup CSS for pages
$screen_css      = "<link rel='stylesheet' href='css/screen.css' media='screen' type='text/css' />";
$create_page_css = "<link rel='stylesheet' href='css/create.css' media='screen' type='text/css' />"; 
$league_home_css = "<link rel='stylesheet' href='css/league_home.css' media='screen' type='text/css' />";
$team_home_css   = "<link rel='stylesheet' href='css/team_home.css' media='screen' type='text/css' />";
$tabs_css        = "<link rel='stylesheet' href='http://stilbuero.de/jquery/tabs_3/ui.tabs.css' media='screen' type='text/css' />";
$welcome_css     = "<link rel='stylesheet' href='css/welcome.css' media='screen' type='text/css' />";
$calendar_css    = "<link rel='stylesheet' href='css/calendar.css' media='screen' type='text/css' />";
$trade_css       = "<link rel='stylesheet' href='css/trade_player.css' media='screen' type='text/css' />";
$free_css        = "<link rel='stylesheet' href='css/free_agent.css' media='screen' type='text/css' />";
$contact_css     = "<link rel='stylesheet' href='css/contact.css' media='screen' type='text/css' />";

//Setup javascript for pages
$tabs_js  = "<script type='text/javascript' src='js/tabs/jquery-1.2.6.js'></script>";
$tabs_js .= "<script type='text/javascript' src='js/tabs/ui.core.js'></script>";
$tabs_js .= "<script type='text/javascript' src='js/tabs/ui.tabs.js'></script>";
$tabs_js .= "<script type='text/javascript' src='js/tabs/helperTabs.js'></script>";

$common_js        = "<script type='text/javascript' src='js/common.js'></script>";
$team_home_js     = "<script type='text/javascript' src='js/team_home.js'></script>";
$welcome_js       = "<script type='text/javascript' src='js/welcome.js'></script>";
$create_team_js   = "<script type='text/javascript' src='js/create_team.js'></script>";
$create_league_js = "<script type='text/javascript' src='js/create_league.js'></script>";
$calendar_js      = "<script type='text/javascript' src='js/calendar_us.js'></script>";
$trade_js         = "<script type='text/javascript' src='js/trade_player.js'></script>";
$popup_js	      = "<script type='text/javascript' src='js/popup.js'></script>";
$free_js          = "<script type='text/javascript' src='js/free_agent.js'></script>";
$league_js        = "<script type='text/javascript' src='js/league_home.js'></script>";
$password_js      = "<script type='text/javascript' src='js/verify_password.js'></script>";
$contact_js       = "<script type='text/javascript' src='js/contact.js'></script>";

$css_league_home = $tabs_css . $screen_css . $league_home_css . $team_home_css;
$css_team_home   = $tabs_css . $screen_css . $team_home_css;
$css_create      = $screen_css . $create_page_css . $calendar_css;
$css_welcome     = $screen_css . $welcome_css;
$css_trade       = $tabs_css . $screen_css . $team_home_css . $trade_css;
$css_free        = $tabs_css . $screen_css . $team_home_css . $trade_css . $free_css;
$css_contact     = $screen_css . $contact_css;

$js_league_home   = $common_js . $tabs_js . $team_home_js . $league_js . $popup_js;
$js_team_home     = $common_js . $team_home_js . $tabs_js . $popup_js;
$js_welcome       = $common_js . $welcome_js . $popup_js;
$js_create_team   = $common_js . $create_team_js . $popup_js;
$js_create_league = $common_js . $create_league_js . $calendar_js . $popup_js;
$js_trade         = $common_js . $team_home_js . $tabs_js . $trade_js;
$js_free          = $common_js . $team_home_js . $tabs_js . $trade_js . $free_js;
$js_password	  = $common_js . $password_js . $popup_js;
$js_contact       = $common_js . $contact_js;

$g_db_name  = "FBT52009";
$g_username = "broomball";

//This sets up the navigation bar for the website.  This makes
//it so it is consistent throughout the website
$g_nav_bar   = "<li><a href='league_home.php'>League</a></li>";
$g_nav_bar  .= "<li><a href='team_home.php'>My Team</a></li>";
$g_nav_bar  .= "<li><a href='trade_player.php'>Trade Players</a></li>";
$g_nav_bar  .= "<li><a href='free_agent.php'>Free Agent</a></li>";

$less_space = false;

//Query to check if a live draft link needs to be in the nav bar
if (isset($_SESSION['team_id'])){
	$query = "SELECT DRAFT,DRAFT_DONE FROM LEAGUES WHERE LEAGUE_ID = {$_SESSION['league_id']}";
	$result = do_query($query, $g_db_name, $g_username);
	$row = mysql_fetch_assoc($result);
	
	if ($row['DRAFT'] == 1 && $row['DRAFT_DONE'] != 1){
		$g_nav_bar  .= "<li><a href='live_draft.php'>Live Draft</a></li>";
        $g_nav_bar  .= "<STYLE type = 'text/css'>
                            #small_contact {
                                left: 380px;
                            }
                            #masthead a{
                                color: #ffffff;
                            }
                         </STYLE>";		
		$less_space = true;
	}
}

//$g_nav_bar  .= "<li><a href=''>How to Play</a></li>";

$g_nav_div     = "<div id='masthead'>";
$g_nav_div    .= "<h1>";
$g_nav_div    .= "</h1>";
$g_nav_div    .= "<ul>";
$g_nav_div    .= $g_nav_bar;
$g_nav_div    .= "</ul>";

if ($less_space)
	$g_nav_div    .= "<div id='small_contact'>";
else
	$g_nav_div    .= "<div id='contact'>";

if(isset($_SESSION['team_id'])){
	$g_nav_div    .= "<span onclick='logOut();'>Log out</span>";
	$g_nav_div    .= " | ";
}
else {
	$g_nav_div    .= "<span onclick='logIn();'>Log in</span>";
	$g_nav_div    .= " | ";
}

$g_nav_div    .= "<span><a href='contact.php'>Contact</a></span>";

$g_nav_div    .= "</div>";
$g_nav_div    .= "</div>";

$footer = "<p>©2009 Michigan Tech. All Rights Reserved.</p>";

//Setup for email scripts
$league_invite_subject = "Fantasy Broomball League Invitation";
$team_creation_subject = "Fantasy Broomball Team Creation";


/**
* @Desc Function to get the current directory the user is viewing 
*       the website from.  This is needed for website redirection
*
* @return the current directory path and server
**/
function cur_web_dir() {
	$ret   = $_SERVER['SERVER_NAME'];
    $index = strrpos($_SERVER['SCRIPT_NAME'],'/');
    $ret  .= substr($_SERVER['SCRIPT_NAME'], 0, $index);

	return $ret;
}


/**
* @Desc Function used to perform the specified query passed in 
*
* @param query mysql query to use for this query to the database
* @param db_name the name of the database to query
* @param username the username to use while querying the database
*
* @return the results from the query (may be null)
*
**/
function do_query($query, $db_name, $username){

	//Setup the MySQL connection data
	$host_name = "orion.csl.mtu.edu";
	//Select the password for the user based on the username passed in
	switch ($username) {
		case "broomball" :
			$password = "broomball";
			break;
	}
	
	//Connect to the database specified
	$link = mysql_connect($host_name, $username, $password) or die("Unable to connect to MySQL DB");
	mysql_select_db($db_name) or die ("Could not select database");
	
	//Store the results of the query 
	$results = mysql_query($query);
	
	//Close the connection to the database
	mysql_close($link);
	
	//Return the results to the caller
	return $results;
}


/**
* @Desc Used to take in an id of a database table entry and return
*       a meaningful identifier from the table entry.  Common queries 
*       are put in this function as cases.
*
* @param type Identifies the case to be selected to perform the database query
* @param value The id to use in the WHERE clause of the query
**/
function get_value($type, $value){
    global $g_db_name, $g_username;
    
    $query = "";
    
    //Select the query to perform based on the type identifier
    switch ($type) {
        
        case "player_id" :
            $query = "SELECT PLAYER_NAME 
                      FROM PLAYERS 
                      WHERE PLAYER_ID = $value";
             break;
       case "team_id" :
             $query = "SELECT TEAM_NAME 
                      FROM TEAMS 
                      WHERE TEAM_ID = $value";
             break;
    }
   
   //Perform the query and return the information desired
   $result = do_query($query, $g_db_name, $g_username);
   $row = mysql_fetch_array($result);
   
   return $row[0];
}


/**
* @Desc Function to send out an email with the specified subject, body, and list of TO emails.
*
* @param subject the subject of the email
* @param body the body of the email
* @param to_list a comma-seperated list of email addresses to send the email to
* @isHTML true if body contains html, false otherwise
*
* @return 0 if successful or -1 if an email could not be sent
**/
function email_message($subject, $body, $to_list, $isHTML = false) {

    $success = 0;
    $failure = -1;

    //Setup the default
    $from = "Fantasy Broomball <fantasybroomball@gmail.com>";
    $to = explode(";",$to_list);
    $host = "smtp.cec.mtu.edu";

    //Loop through the list of emails to send to
	for ($i = 0; $i < sizeof($to); $i++){
		$headers = array ('From' => $from,
		  'To' => $to[$i],
		  'Subject' => $subject);
		$smtp = Mail::factory('smtp',
		  array ('host' => $host));

		$mail = $smtp->send($to[$i], $headers, $body);

		if (PEAR::isError($mail)) { 
		  return $failure;
		}
	}

	return $success;
}

?>
